import { PORT3, PORT4 } from "@/api/config/servicePort";
import http from "@/api";
import { Customer, Dictionary, Location, Material } from "../interface/basic";
import { childrenProps } from "@/components/ProForm/interface";

// 测试接口目前用于检测token是否过期
export const verifyToken = () => http.post(PORT3 + `/test/checkToken`);

// 得到系统下指定名称的配置项
export const systemByNames = params => http.get(PORT4 + `/system/byNames`, params);

// 分页查询客户
export const customerPage = (params: Customer.ReqSearchParams) => http.post<Customer.ReqSearch>(PORT3 + `/customer/page`, params);

// 分页查询物料池
export const materialPage = (params: Material.ResSearchParams) => http.post(PORT3 + `/material/page`, params);

// 打印标签
export const printLabel = async (params: { isBatch?: boolean; [key: string]: any }) => {
  const printData = await systemByNames({ names: "receive_print_address" });
  const url = `${printData["receive_print_address"]}/boot/print` + (params.isBatch ? "s" : "");
  return http.post(url, params);
};

// 多个请求同时请求
export const promiseAllApi = async (reqArr: Array<Promise<unknown>>) => {
  const resArr = await Promise.all(reqArr);
  return resArr;
};

// 查询字典值
export const dictionaryListByCodes = (params: { code: string }) =>
  http.get<Dictionary.ResSearch[]>(PORT3 + `/dictionary/listByCodes`, params);

// 查询可用的位置树结构
export const locationEnableTree = async () => {
  const children = await http.get<Location.ResTreeProps[]>(PORT3 + `/location/enableTree`);
  let newChildren: childrenProps[] = [];
  for (let item of children) {
    const { key, value } = item;
    newChildren.push({
      text: key,
      value: key,
      children: value.map(son => {
        const { location, id } = son;
        return {
          text: location,
          value: id + ""
        };
      })
    });
  }
  return newChildren;
};
